package com.alkandros.footballsounds;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	
	private Button goHawks;
	private Button tdSeaHawks;
	private Button legionBoom;
	private Button twelthMan;
	private Button joshBrown;
	private Button fortyWhiners;
	private TextView btnRecord;
	private TextView btnPlay;
	private TextView txtHeading;
	
	private String btnRecordTag   = "Record";
	private String btnStopTag     = "Stop";
	private MediaRecorder recorder;
	private String AUDIO_RECORDER_FOLDER = "SeaHawks";
	private String filepath="";
	MediaPlayer mp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialiseUI();
		setFonts();
		setButtons();
	}
	private void initialiseUI()
	{
		goHawks    		= (Button)findViewById(R.id.gohawks);
		tdSeaHawks    	= (Button)findViewById(R.id.touchDown);
		legionBoom  	= (Button)findViewById(R.id.legionBoom);
		twelthMan    	= (Button)findViewById(R.id.twelthMan);
		joshBrown    	= (Button)findViewById(R.id.joshBrown);
		fortyWhiners  	= (Button)findViewById(R.id.fortyWhiners);
		btnRecord       = (TextView)findViewById(R.id.record);
		btnPlay	        = (TextView)findViewById(R.id.play);
		txtHeading      = (TextView)findViewById(R.id.textHeading);
		
		btnRecord.setTag(btnRecordTag);
	}
	private void setButtons()
	{
		goHawks.setOnClickListener(this);
		tdSeaHawks.setOnClickListener(this);
		legionBoom.setOnClickListener(this);
		twelthMan.setOnClickListener(this);
		joshBrown.setOnClickListener(this);
		fortyWhiners.setOnClickListener(this);
		btnRecord.setOnClickListener(this);
		btnPlay.setOnClickListener(this);
		
		SelectorManager.setBackground(goHawks, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
		SelectorManager.setBackground(tdSeaHawks, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
		SelectorManager.setBackground(legionBoom, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
		SelectorManager.setBackground(twelthMan, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
		SelectorManager.setBackground(joshBrown, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
		SelectorManager.setBackground(fortyWhiners, SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.button_bg, R.drawable.button_press));
	}
	private void setFonts()
	{
		Typeface face=Typeface.createFromAsset(getAssets(),"fonts/font.ttf");
		goHawks.setTypeface(face);
		tdSeaHawks.setTypeface(face);
		legionBoom.setTypeface(face);
		twelthMan.setTypeface(face);
		joshBrown.setTypeface(face);
		txtHeading.setTypeface(face);
		fortyWhiners.setTypeface(face);
		btnRecord.setTypeface(face);
		btnPlay.setTypeface(face);
	}
	@Override
	public void onClick(View v) 
	{
		if(v==fortyWhiners)
		{
			playAudio(R.raw.forty_whiners);
		}
		else if(v==legionBoom)
		{
			playAudio(R.raw.legion_of_boom);
		}
		else if(v==goHawks)
		{
			playAudio(R.raw.go_hawks);
		}
		else if(v==twelthMan)
		{
			playAudio(R.raw.twelth_man);
		}
		else if(v==tdSeaHawks)
		{
			playAudio(R.raw.td_seahawks);
		}
		else if(v==joshBrown)
		{
			playAudio(R.raw.boo);
		}
		else if(v==btnRecord)
		{
			if(btnRecord.getTag().equals(btnRecordTag))
			{
				enableDisable(false);
				btnRecord.setText(btnStopTag);
				btnRecord.setTag(btnStopTag);
				startRecording();
				btnRecord.setTextColor(Color.RED);
			}
			else
			{
				btnRecord.setTextColor(Color.WHITE);
				stopRecording();
				enableDisable(true);
				btnRecord.setText(btnRecordTag);
				btnRecord.setTag(btnRecordTag);
			}
		}
		else if(v==btnPlay)
		{
			if(!getFilename().equals("") && btnRecord.getTag().equals(btnRecordTag))
			{
				playRecordedAudio();
			}
		}
	}
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		filepath ="";
		if(mp!=null)
		{
			mp.release();	
		}
	}
	private void playRecordedAudio()
	{
		try
		{
			if(mp==null)
			{
				mp = new MediaPlayer();
			}
			if(mp.isPlaying())
			{
				mp.stop();
			}
			Log.e("", "File path"+getFilename());
			mp  = MediaPlayer.create(this, Uri.parse(getFilename()));
			mp.start();
		}
		catch(Exception e)
		{
			
		}
	}
	private void playAudio(int resId)
	{
		if(mp==null)
		{
			mp = new MediaPlayer();
		}
		if(mp.isPlaying())
		{
			mp.stop();
		}
		mp  = MediaPlayer.create(this, resId);
		mp.start();
	}
	private void enableDisable(boolean enable)
	{
		goHawks.setOnClickListener(enable?this:null);
		tdSeaHawks.setOnClickListener(enable?this:null);
		legionBoom.setOnClickListener(enable?this:null);
		twelthMan.setOnClickListener(enable?this:null);
		joshBrown.setOnClickListener(enable?this:null);
		fortyWhiners.setOnClickListener(enable?this:null);
		
		btnPlay.setOnClickListener(enable?this:null);
	}
	public  void startRecording()
    {
		if(mp!=null)
		{
			mp.stop();
		}
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(getFilename());
         //   recorder.setOnErrorListener(errorListener);
       //     recorder.setOnInfoListener(infoListener);
            try 
            {
                    recorder.prepare();
                    recorder.start();
            } 
            catch (IllegalStateException e) 
            {
                    e.printStackTrace();
            } catch (IOException e) 
            {
                    e.printStackTrace();
            }
    }
	 private void stopRecording()
	    {
	        if(null != recorder)
	         {     
	             recorder.stop();
	             recorder.reset();
	             recorder.release();
	             recorder = null;
	         }
	    }
	 private String getFilename()
     {
             filepath = Environment.getExternalStorageDirectory().getPath();
             File file = new File(filepath,AUDIO_RECORDER_FOLDER);
             if(!file.exists())
             {
                     file.mkdirs();
             }
             return (file.getAbsolutePath()+"/"+"1"+".mp3");
     }
}
